package cn.edu.neusoft.yuejian4.yuejian_4.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.MemoryPolicy;
import com.squareup.picasso.Picasso;

import java.util.List;

import cn.edu.neusoft.yuejian4.yuejian_4.Config.Server;
import cn.edu.neusoft.yuejian4.yuejian_4.R;
import cn.edu.neusoft.yuejian4.yuejian_4.adapter.MasterClassListAdapter;
import cn.edu.neusoft.yuejian4.yuejian_4.adapter.MasterQuestionAdapter;
import cn.edu.neusoft.yuejian4.yuejian_4.javaBean.Master;
import cn.edu.neusoft.yuejian4.yuejian_4.javaBean.MasterClass;
import cn.edu.neusoft.yuejian4.yuejian_4.listener.List_TListener;
import cn.edu.neusoft.yuejian4.yuejian_4.listener.TListener;
import cn.edu.neusoft.yuejian4.yuejian_4.model.MasterModel;

/**
 * Created by Lix on 2018/10/18.
 */

public class MasterDetailActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView master_name, master_intro, master_club;
    private RecyclerView recyclerView_class, recyclerView_question;
    private ImageView master_img, back;
    private Button like;
    private SharedPreferences sp;
    private Context mContext;
    private int mid;
    private int uid;
    private Integer integer_mid;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_master_details);
        initView();
        initEvent();
        mid = getIntent().getIntExtra("master_id",1);
        MasterModel masterModel = new MasterModel();
        masterModel.getMaster(mid,getMasterListener);
        masterModel.getMasterClass(mid,getClassListener);
        masterModel.getMasterQuestion(mid,getQuestionListener);
    }
    TListener<Master> getMasterListener = new TListener<Master>() {
        @Override
        public void onResponse(Master master) {
            Picasso.with(MasterDetailActivity.this)
                    .load(Server.PIC_URL_J+master.getPic())
                    .placeholder(R.drawable.ic_default_img)
                    .memoryPolicy(MemoryPolicy.NO_CACHE)
                    .into(master_img);
            master_name.setText(master.getName());
            master_intro.setText(master.getIntro());

        }

        @Override
        public void onFail(String msg) {

        }
    };
    List_TListener<MasterClass> getClassListener = new List_TListener<MasterClass>() {
        @Override
        public void onResponse(List<MasterClass> list) {
            MasterClassListAdapter masterClassListAdapter = new MasterClassListAdapter(getApplicationContext(),list);
            recyclerView_class.setAdapter(masterClassListAdapter);
        }

        @Override
        public void onFail(String msg) {

        }
    };

    List_TListener<Master>  getQuestionListener = new List_TListener<Master>() {
        @Override
        public void onResponse(List<Master> list) {
            MasterQuestionAdapter masterQuestionAdapter = new MasterQuestionAdapter(getApplicationContext(),list);
            recyclerView_question.setAdapter(masterQuestionAdapter);
        }

        @Override
        public void onFail(String msg) {

        }
    };

    private void initView() {
        master_name = (TextView) findViewById(R.id.master_details_name);
        master_intro = (TextView) findViewById(R.id.master_detail_intro);
        master_img = (ImageView) findViewById(R.id.master_details_img);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this.getApplicationContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        LinearLayoutManager layoutManager1 = new LinearLayoutManager(this.getApplicationContext());
        layoutManager1.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView_class = (RecyclerView) findViewById(R.id.rv_master_class);
        recyclerView_class.setLayoutManager(layoutManager);
        recyclerView_question = (RecyclerView) findViewById(R.id.rv_master_question);
        recyclerView_question.setLayoutManager(layoutManager1);

        like = (Button)findViewById(R.id.btn_master_like);
        back = (ImageView) findViewById(R.id.master_detail_back);
    }

    private void initEvent() {
        back.setOnClickListener(this);
        like.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.master_detail_back:
                finish();
                break;
        }
    }
}
